//
//  SendMessageTableViewCell.m
//  EasyChat
//
//  Created by romana on 16/6/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SendMessageTableViewCell.h"
#import "Masonry.h"

@implementation SendMessageTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self doIt];
    }
    return self;
    
}


-(void)doIt{
    
    //    self.myLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    //    self.myLabel.backgroundColor = [UIColor clearColor];
    //    self.myLabel.layer.masksToBounds = YES;
    //    self.myLabel.layer.cornerRadius = 10;
    //    self.myLabel.font = [UIFont systemFontOfSize:18];
    //    self.myLabel.numberOfLines = 0;
    // [self.myLabel setNumberOfLines:0];
    
    
    // [self.contentView addSubview:self.myLabel];
    
    self.headImageView = [UIImageView new];
    self.headImageView.backgroundColor = [UIColor whiteColor];
    //  self.myLabel.layer.cornerRadius = 10;
    self.headImageView.image = [UIImage imageNamed:@"chatListHead"];
    [self.contentView addSubview:self.headImageView];
    
    self.backImageView = [UIImageView new];
    self.backImageView.backgroundColor = [UIColor colorWithRed:145 / 255.0 green:226 / 255.0 blue:86 / 255.0 alpha:1.0];
    self.backImageView.layer.cornerRadius = 10;
    [self.contentView addSubview:self.backImageView];
    
    self.myLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.myLabel.backgroundColor = [UIColor clearColor];
    self.myLabel.layer.masksToBounds = YES;
    self.myLabel.layer.cornerRadius = 10;
    self.myLabel.font = [UIFont systemFontOfSize:18];
    self.myLabel.numberOfLines = 0;
    [self.contentView addSubview:self.myLabel];
    
    
}

-(void)layoutSubviews{
    
    [super layoutSubviews];
    self.headImageView.frame = CGRectMake(self.contentView.frame.size.width - 35, 5, 30, 30);
    
//    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18],NSFontAttributeName, nil];
//    
//    CGRect rect = [self.myLabel.text boundingRectWithSize:CGSizeMake(self.frame.size.width / 2 - 40, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
//    self.myLabel.frame = CGRectMake(60 , 20, self.contentView.frame.size.width / 4 * 3 - 50, rect.size.height);
//    
//    self.myLabel.preferredMaxLayoutWidth = self.contentView.frame.size.width / 4 * 3 - 50;
//    
//    [self.myLabel sizeToFit];
//    
//    [self.myLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//       
//        make.right.mas_equalTo(self.headImageView).offset(-55);
//        make.top.mas_equalTo(20);
//        
//    }];
//    
//    
//    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.top.left.mas_equalTo(self.myLabel).offset(-10);
//        make.right.mas_equalTo(self.myLabel).offset(10);
//        make.bottom.mas_equalTo(self.myLabel).offset(-10);
//        
//    }];
    
}

//-(CGFloat)CellHeight{
//
//    //重新布局
//    [self layoutIfNeeded];
//    
//    return 20 + self.myLabel.bounds.size.height;
//
//
//}

-(void)setMessage:(EMMessage *)message{
    
    id body = message.messageBodies[0];
    if ([body isKindOfClass:[EMTextMessageBody class]]) {//文本消息
        EMTextMessageBody *textBody = body;
        self.myLabel.text = textBody.text;
        
        
        
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18],NSFontAttributeName, nil];
        
        CGRect rect = [self.myLabel.text boundingRectWithSize:CGSizeMake(self.frame.size.width / 2 - 40, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        self.myLabel.frame = CGRectMake(60 , 20, self.contentView.frame.size.width / 4 * 3 - 50, rect.size.height);
        
        self.myLabel.preferredMaxLayoutWidth = self.contentView.frame.size.width / 4 * 3 - 50;
        
        [self.myLabel sizeToFit];
        
        [self.myLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.mas_equalTo(self.headImageView).offset(-55);
            make.top.mas_equalTo(20);
            
        }];
        
        
        [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.left.mas_equalTo(self.myLabel).offset(-10);
            make.right.mas_equalTo(self.myLabel).offset(10);
            make.bottom.mas_equalTo(self.myLabel).offset(-10);
            
        }];

        
        
        
        
<<<<<<< HEAD
        //图片富文本
        NSAttributedString *imgAtt = [NSAttributedString attributedStringWithAttachment:imageAttachment];
        [voiceAttM appendAttributedString:imgAtt];
        
        self.myLabel.attributedText = voiceAttM;
        
    }else if([body isKindOfClass:[EMImageMessageBody class  ]]){
        
      //  self.myLabel.frame = CGRectMake(0, 0, 200 , 200);
        [self.myLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.mas_equalTo(self.headImageView).offset(-55);
            make.top.mas_equalTo(20);
            
        }];
        
        [self showImage];
        NSLog(@"这是图片");
    
    }else
    {self.myLabel.text = @"未知类型";

=======
>>>>>>> c579263d575b1508bad7ec7b2eed5c33d049d24a
        
        
        
        
        
        
    }else{
        self.myLabel.text = @"未知类型";
    }
<<<<<<< HEAD
        
        
        
//    }else{
//        self.myLabel.text = @"未知类型";
//    }
    

}

-(void)showImage{

    
    //获取图片消息体
    EMImageMessageBody *imgBody = self.message.messageBodies[0];
    
    CGRect thumbnailFrame = (CGRect){0,0,imgBody.thumbnailSize};
    
    

    
    //设置label的尺寸足够现实uiimageView
    NSTextAttachment *imgAttach = [[NSTextAttachment alloc] init];
    
    UIImage *showImage = [[UIImage alloc]init];
    
    imgAttach.bounds = thumbnailFrame;
    

    
//1.cell里添加一个UIimageView
    UIImageView *chatImageView = [[UIImageView alloc] init];
    [self.myLabel addSubview:chatImageView];
    chatImageView.backgroundColor = [UIColor redColor];
    
    
    //2.设置图片控件为缩略图的尺寸

  //  chatImageView.frame = thumbnailFrame;
   
    
    //3.下载图片
    
    NSFileManager *manager = [NSFileManager defaultManager];
//如果本地图片存在,直接从本地显示图片
    if ([manager fileExistsAtPath:imgBody.thumbnailLocalPath]) {
      //  [chatImageView sd_setImageWithURL:[NSURL fileURLWithPath:imgBody.thumbnailLocalPath]];
        
        UIImage *image = [UIImage imageWithContentsOfFile:imgBody.thumbnailLocalPath];
        
        showImage = image;
        
        
        NSLog(@"%@",imgBody.thumbnailLocalPath);
    }else{
        //如果本地图片不存在,
    //  [chatImageView sd_setImageWithURL:[NSURL URLWithString:imgBody.thumbnailLocalPath]];
        
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgBody.thumbnailRemotePath]];
        UIImage *image = [UIImage imageWithData:imageData];
        showImage = image;
    
    }
    
    imgAttach.image = showImage;

    NSAttributedString *imgAtt = [NSAttributedString attributedStringWithAttachment:imgAttach];
    self.myLabel.attributedText = imgAtt;
    
=======
    
    
    
>>>>>>> c579263d575b1508bad7ec7b2eed5c33d049d24a
}

@end
